package com.health.mapp.service;

import com.alibaba.fastjson.JSONObject;
import com.health.db.model.UserLIST;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.impl.GetStuffOpenIdImpl;
import com.health.mapp.utils.StringUtils;
import com.wxtools.api.IService;
import com.wxtools.bean.CustomerSender;
import com.wxtools.bean.SenderContent;
import com.wxtools.bean.TemplateSender;
import com.wxtools.bean.WxOpenidSender;
import com.wxtools.bean.result.SenderResult;
import com.wxtools.bean.result.TemplateSenderResult;

import org.apache.commons.collections.ListUtils;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.awt.*;
import java.io.Console;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendWXTest extends HttpServlet{

    private IService mpService;
    private IUserLISTService userLISTService;

    private static Logger logger = Logger.getLogger(SendWXTest.class);


    public void init(ServletConfig config) throws ServletException {
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
                config.getServletContext());
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String userId=request.getParameter("userId");
        if (StringUtils.isBlank(userId))
        {
            logger.error("userId 参数不能为空");
            return;
        }
        ServletContext servletContext = request.getSession().getServletContext();
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        mpService = (IService) applicationContext.getBean("mpService");userLISTService= (IUserLISTService) applicationContext.getBean("userLISTService");

        UserLIST  userLIST=userLISTService.selectByUserId(Long.valueOf(userId));
        if (null==userLIST)
        {
            logger.error("没查下到用户");
            return;
        }
        if (StringUtils.isBlank(userLIST.getWxopenid()))
        {
            logger.error("openId为空");
            return;
        }

        /*CustomerSender wxOpenidSender=new CustomerSender();
        wxOpenidSender.setTouser(userLIST.getWxopenid());
        wxOpenidSender.setMsgtype("text");
        SenderContent.Text text=new SenderContent.Text("测试消息11");
        wxOpenidSender.setText(text);

        try {
            SenderResult senderResult=mpService.sendCustomserByOpenid(wxOpenidSender);
            logger.info(senderResult.toString());
        }catch (Exception e)
        {
            logger.error(e.getMessage());
        }*/
        
        TemplateSender sender = new TemplateSender();
        sender.setTouser(userLIST.getWxopenid());
        sender.setTemplate_id("N0TEnfbA4HYP47g_6D3XDJ-H2emfsWyXX1P7-8vyXxo");
        //Map<String, String> map= new HashMap<String, String>();
        
        JSONObject keyword1 = new JSONObject();
        keyword1.put("color", "#173177");
        keyword1.put("value", "基本信息测试");
        
        JSONObject first = new JSONObject();
        first.put("color", "#173177");
        first.put("value", "标题测试");
        
        JSONObject keyword2 = new JSONObject();
        keyword2.put("color", "#173177");
        keyword2.put("value", "结果");
        
        JSONObject keyword3 = new JSONObject();
        keyword3.put("color", "#173177");
        keyword3.put("value", "2018-1-24 23:00:00");
        
        JSONObject keyword4 = new JSONObject();
        keyword4.put("color", "#173177");
        keyword4.put("value", "基本信息基本信息基本信息基本信息");
        
        JSONObject remark = new JSONObject();
        remark.put("color", "#173177");
        remark.put("value", "咨询XXX医生");
        
        JSONObject map = new JSONObject();
        map.put("first", first);
        map.put("keyword1", keyword1);
        map.put("keyword2",keyword2);
        map.put("keyword3", keyword3);
        map.put("keyword4", keyword4);
        map.put("remark", remark);
        sender.setData(map);
        
        try {
        	TemplateSenderResult senderResult=mpService.templateSend(sender);
        	 //TemplateSenderResult result = iService.templateSend(sender);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
